<?php

namespace backend\modules\i18n\controllers;

use Yii;
use backend\modules\i18n\models\I18nSourceMessage;
use backend\modules\i18n\models\I18nMessage;
use backend\modules\i18n\models\search\I18nSourceMessageSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * I18nSourceMessageController implements the CRUD actions for I18nSourceMessage model.
 */
class I18nSourceMessageController extends Controller {

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all I18nSourceMessage models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new I18nSourceMessageSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single I18nSourceMessage model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        $i18nMessage = new I18nMessage();
        $model = $this->findModel($id);
        $model->language = $i18nMessage->findOne($id)->language;
        $model->translation = $i18nMessage->findOne($id)->translation;
        return $this->render('view', [
                    'model' => $model,
        ]);
    }

    /**
     * Creates a new I18nSourceMessage model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new I18nSourceMessage();

        $i18nMessage = new I18nMessage();
        if ($model->load(Yii::$app->request->post())) {
            $model->category = $_POST['I18nSourceMessage']['category'];
            $model->message = $_POST['I18nSourceMessage']['message'];
            $model->save();
            $i18nMessage->id = $model->id;
            $i18nMessage->language = $_POST['I18nSourceMessage']['language'];
            $i18nMessage->translation = $_POST['I18nSourceMessage']['translation'];
            if ($i18nMessage->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing I18nSourceMessage model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $i18nMessage = new I18nMessage();
        $model->language = $i18nMessage->findOne($id)->language;
        $model->translation = $i18nMessage->findOne($id)->translation;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->category = $_POST['I18nSourceMessage']['category'];
            $model->message = $_POST['I18nSourceMessage']['message'];
            $model->save();
            $i18nMessage->id = $model->id;
            $i18nMessage->language = $_POST['I18nSourceMessage']['language'];
            $i18nMessage->translation = $_POST['I18nSourceMessage']['translation'];
            if ($i18nMessage->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            return $this->render('update', [
                        'model' => $model,                        
            ]);
        }
    }

    /**
     * Deletes an existing I18nSourceMessage model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the I18nSourceMessage model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return I18nSourceMessage the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = I18nSourceMessage::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionCome() {
        $arr = [
    'API access token' => 'Token de acceso de la API',
    'Email' => 'Correo',
    'Expire At' => 'Expira el',
    'Not Active' => 'Inactivo',
    'Password' => 'Contraseña',
    'Roles' => 'Roles',
    'Token' => 'Token',
    '"{attribute}" must be a valid JSON' => '"{attribute}" debe de ser JSON válido',
    'Active' => 'Activo',
    'Article ID' => 'ID Artículo',
    'Article View' => 'Vista de Artículo',
    'Author' => 'Autor',
    'Base URL' => 'URL Base',
    'Base Url' => 'Url Base',
    'Body' => 'Cuerpo',
    'Caption' => 'Leyenda',
    'Carousel ID' => 'Carrusel ID',
    'Category' => 'Categoría',
    'Comment' => 'Comentario',
    'Component' => 'Componente',
    'Config' => 'Configuración',
    'Created At' => 'Creado el',
    'Created at' => 'Creado el',
    'Deleted' => 'Borrado',
    'Down to maintenance.' => 'Apagado por mantenimiento.',
    'E-mail' => 'E-mail',
    'File Type' => 'Tipo fichero',
    'Firstname' => 'Nombre',
    'Gender' => 'Género',
    'ID' => 'ID',
    'Image' => 'Imagen',
    'Key' => 'Clave',
    'Last login' => 'ültimo Login',
    'Lastname' => 'Apellido',
    'Locale' => 'Pais',
    'Middlename' => 'Segundo Nombre',
    'Name' => 'Nombre',
    'Order' => 'Orden',
    'Page View' => 'Vista de Página',
    'Parent Category' => 'Categoría Padre',
    'Path' => 'Ruta',
    'Picture' => 'Imagen',
    'Published' => 'Publicado',
    'Published At' => 'Publicado el',
    'Size' => 'Tamaño',
    'Slug' => 'Slug',
    'Status' => 'Estado',
    'Thumbnail' => 'Miniatura',
    'Title' => 'Título',
    'Type' => 'Tipo',
    'Updated At' => 'Actualizado el',
    'Updated at' => 'Actualizado el',
    'Updater' => 'Actualizado por',
    'Upload Ip' => 'IP',
    'Url' => 'Url',
    'User ID' => 'Usuario ID',
    'Username' => 'Nombre Usuario',
    'Value' => 'Valor',
];
        
//        foreach ($arr as $key=>$val){
//            $model = new I18nSourceMessage();
//            $i18nMessage = new I18nMessage();
//            $model->category = 'common';
//            $model->message = $key;
//            $model->save();
//            $i18nMessage->id = $model->id;
//            $i18nMessage->language = 'es';
//            $i18nMessage->translation = $val;
//            $i18nMessage->save();
//        }
    }

}
